import time
import api
import json

if __name__ == '__main__':
    num = 0
    api.sendNoticeMessage("开始自动领取红包咯")
    while num < 10:
        # json_str = api.getRedPacketFall().replace("'", "\"")
        # data = json.loads(json_str)
        data = api.getRedPacketFall()
        data = data['data']['list']
        for ele in data:
            print(ele['itemType'])
            # 博客红包领取
            if ele['itemType'] == "article":
                # 判断是否被领取过
                print(ele['checkRedPacket']['status'])
                if ele['checkRedPacket']['status'] == "unreceived":
                    print('未领取')
                    result = api.receiveRedPacketForBlogAndBlink(ele['checkRedPacket']['orderNo'])
                    if json.loads(result)['code'] == 200:
                        num = num + 1
                        money = json.loads(result)['data']['redPacketMoney']
                        api.sendNoticeMessage(str(money))
                        time.sleep(121)
                    elif json.loads(result)['code'] == 401:
                        if json.loads(result)['message'] == "登陆验证失败，请重新登陆":
                            print("刷新Cookie")
                            api.refreshLogin()
                        else:
                            api.sendNoticeMessage(result)
                    else:
                        time.sleep(121)
                        continue
                else:
                    print("已经领取过了")
                    continue
            # 社区红包领取
            elif ele['itemType'] == "topic":
                # 判断是否被领取过
                if ele['content']['checkRedPacket']['msg'] == "unreceived":
                    print(ele['content']['checkRedPacket']['contractConfig']['resourceId'])
                    print(ele['content']['checkRedPacket']['contractConfig']['communityId'])
                    print(ele['content']['checkRedPacket']['postId'])
                    result = api.receiveRedPacketForCommunity(ele['content']['checkRedPacket']['contractConfig']['resourceId'],ele['content']['checkRedPacket']['contractConfig']['communityId'],ele['content']['checkRedPacket']['postId'])
                    if json.loads(result)['code'] == 200:
                        num = num + 1
                        money = json.loads(result)['data']['redPacketMoney']
                        api.sendNoticeMessage(str(money))
                        time.sleep(121)
                    elif json.loads(result)['code'] == 401:
                        if json.loads(result)['message'] == "登陆验证失败，请重新登陆":
                            print("刷新Cookie")
                            api.refreshLogin()
                        else:
                            api.sendNoticeMessage(result)
                    else:
                        time.sleep(121)
                        continue
                else:
                    continue
            # blink红包领取
            elif ele['itemType'] == "blink":
                # 判断是否被领取过
                if ele['checkRedPacket']['status'] == "unreceived":
                    result = api.receiveRedPacketForBlogAndBlink(ele['checkRedPacket']['orderNo'])
                    if json.loads(result)['code'] == 200:
                        num = num + 1
                        money = json.loads(result)['data']['redPacketMoney']
                        api.sendNoticeMessage(str(money))
                        time.sleep(121)
                    elif json.loads(result)['code'] == 401:
                        if json.loads(result)['message'] == "登陆验证失败，请重新登陆":
                            print("刷新Cookie")
                            api.refreshLogin()
                        else:
                            api.sendNoticeMessage(result)
                    else:
                        time.sleep(121)
                        continue
                else:
                    continue
    api.sendNoticeMessage("自动领取红包结束")
